#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTableWidgetItem>
#include <QRandomGenerator>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    labCellIndex = new QLabel("当前单元格坐标：", this);
    labCellIndex->setMinimumWidth(250);
    labCellType = new QLabel("当前单元格类型：", this);
    labCellType->setMinimumWidth(200);
    labStudID = new QLabel("学生ID", this);
    labStudID->setMinimumWidth(200);

    ui->statusbar->addWidget(labCellIndex);
    ui->statusbar->addWidget(labCellType);
    ui->statusbar->addWidget(labStudID);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_btnSetHeader_clicked()
{
    ui->tableInfo->clear();
    QStringList tabInfo = {"姓名", "性别", "出生日期", "民族", "分数", "是否党员"};
    ui->tableInfo->setColumnCount(tabInfo.size());
    for(int i = 0; i < tabInfo.size(); ++i)
    {
        QTableWidgetItem* item = new QTableWidgetItem;
        item->setText(tabInfo.at(i));
        //item->setBackground(QBrush(Qt::yellow));
        QFont font = item->font();
        font.setBold(true);
        font.setPixelSize(11);
        item->setFont(font);
        item->setForeground(QBrush(Qt::red));
        item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        ui->tableInfo->horizontalHeader()->setStyleSheet(
            "QHeaderView::section {"
            "background-color: yellow;"
            // "color: red;"
            // "font-weight: bold;"
            "}");
        //ui->tableInfo->horizontalHeader()->setStretchLastSection(true);
        ui->tableInfo->setHorizontalHeaderItem(i, item);
    }
    ui->tableInfo->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
}

void MainWindow::on_btnSetRows_clicked()
{
    //ui->tableInfo->clearContents();
    ui->tableInfo->setRowCount(ui->spinRowCount->value());
}

void MainWindow::on_btnIniData_clicked()
{
    int rowCount = ui->spinRowCount->value();
    ui->tableInfo->clearContents();
    ui->tableInfo->setRowCount(rowCount);
    for(int i = 0; i < rowCount; ++i)
    {
        QString name;
        QString sex;
        QDate birth;
        QString nation = "汉族";
        bool isPM = true;
        int score = QRandomGenerator::global()->bounded(60, 101);

        CreatData(i, name, sex, birth, nation, isPM, score);
        CreatItemArow(i, name, sex, birth, nation, isPM, score);
    }

    if(!ui->chkBoxTabEditable->isChecked())
    {
        ui->tableInfo->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }

    ui->tableInfo->setAlternatingRowColors(ui->chkBoxRowColor->isChecked());
    ui->tableInfo->horizontalHeader()->setVisible(ui->chkBoxHeaderH->isChecked());
    ui->tableInfo->verticalHeader()->setVisible(ui->chkBoxHeaderV->isChecked());
    if(ui->rBtnSelectItem->isChecked())
    {
        ui->tableInfo->setSelectionBehavior(QAbstractItemView::SelectItems);
    }
    else
    {
        ui->tableInfo->setSelectionBehavior(QAbstractItemView::SelectRows);
    }
}

void MainWindow::CreatItemArow(int rowNo, QString name, QString sex, QDate birthDay, QString nation, bool isPM, int score)
{
    QTableWidgetItem* nameItem = new QTableWidgetItem(ctName);
    nameItem->setText(name);

    QTableWidgetItem* sexItem = new QTableWidgetItem(ctSex);
    sexItem->setText(sex);
    if(sex == "男")
    {
        QIcon icon("..\\..\\icons\\boy.ico");
        sexItem->setIcon(icon);
    }
    else
    {
        QIcon icon("..\\..\\icons\\girl.ico");
        sexItem->setIcon(icon);
    }

    QTableWidgetItem *birthItem = new QTableWidgetItem(ctBirth);
    birthItem->setText(birthDay.toString("yyyy-MM-dd"));

    QTableWidgetItem *nationItem = new QTableWidgetItem(ctNation);
    nationItem->setText(nation);

    QTableWidgetItem *isPmItem = new QTableWidgetItem(ctPartyM);
    if(isPM)
        isPmItem->setText("党员");
    else
        isPmItem->setText("非党员");
    if(sex == "女")
        isPmItem->setCheckState(Qt::Checked);
    else
        isPmItem->setCheckState(Qt::Unchecked);

    QTableWidgetItem *scoreItem = new QTableWidgetItem(ctScore);
    scoreItem->setText(QString::number(score));

    // if(!ui->chkBoxTabEditable->isChecked())
    // {
    //     nameItem->setFlags(nameItem->flags() & ~Qt::ItemIsEditable);
    //     sexItem->setFlags(sexItem->flags() & ~Qt::ItemIsEditable);
    //     birthItem->setFlags(birthItem->flags() & ~Qt::ItemIsEditable);
    //     nationItem->setFlags(nationItem->flags() & ~Qt::ItemIsEditable);
    //     isPmItem->setFlags(isPmItem->flags() & ~Qt::ItemIsEditable);
    //     scoreItem->setFlags(scoreItem->flags() & ~Qt::ItemIsEditable);
    // }

    ui->tableInfo->setItem(rowNo, colName, nameItem);
    ui->tableInfo->setItem(rowNo, colSex, sexItem);
    ui->tableInfo->setItem(rowNo, colBirth, birthItem);
    ui->tableInfo->setItem(rowNo, colNation, nationItem);
    ui->tableInfo->setItem(rowNo, colPartyM, isPmItem);
    ui->tableInfo->setItem(rowNo, colScore, scoreItem);
}

void MainWindow::CreatData(const int &No, QString &name, QString &sex, QDate &birthDay, QString &nation, bool &isPM, int &score)
{
    name = "学生 " + QString::number(No + 1);
    if(No % 2 == 1)
        sex = "男";
    else
        sex = "女";

    int year = QRandomGenerator::global()->bounded(1990, 2003);
    int month = QRandomGenerator::global()->bounded(1, 13);
    int day = QRandomGenerator::global()->bounded(1, 31);
    birthDay.setDate(year, month, day);

    nation = "汉族";
    isPM = true;
    score = QRandomGenerator::global()->bounded(60, 101);
}

void MainWindow::on_btnInsertRow_clicked()
{
    int curRow = ui->tableInfo->currentRow();
    ui->tableInfo->insertRow(curRow);

    QString name;
    QString sex;
    QDate birth;
    QString nation = "汉族";
    bool isPM = true;
    int score = QRandomGenerator::global()->bounded(60, 101);

    CreatData(curRow, name, sex, birth, nation, isPM, score);
    CreatItemArow(curRow, name, sex, birth, nation, isPM, score);

    for(int i = curRow + 1; i < ui->tableInfo->rowCount(); ++ i)
    {
        auto item = ui->tableInfo->item(i, colName);
        if(item != nullptr)
         item->setText("学生 " + QString::number(i + 1));
    }
}

void MainWindow::on_btnAppendRow_clicked()
{
    int lastRowNo = ui->tableInfo->rowCount();
    ui->tableInfo->insertRow(lastRowNo);

    QString name;
    QString sex;
    QDate birth;
    QString nation = "汉族";
    bool isPM = true;
    int score = QRandomGenerator::global()->bounded(60, 101);

    CreatData(lastRowNo, name, sex, birth, nation, isPM, score);
    CreatItemArow(lastRowNo, name, sex, birth, nation, isPM, score);
}

void MainWindow::on_btnDelCurRow_clicked()
{
    int curRow = ui->tableInfo->currentRow();
    ui->tableInfo->removeRow(curRow);
}

void MainWindow::on_btnAutoHeght_clicked()
{
    ui->tableInfo->resizeRowsToContents();
}

void MainWindow::on_btnAutoWidth_clicked()
{
    ui->tableInfo->resizeColumnsToContents();
}

void MainWindow::on_btnReadToEdit_clicked()
{
    QString header;
    for(int i = 0; i < ui->tableInfo->columnCount(); ++i)
    {
        header += ui->tableInfo->horizontalHeaderItem(i)->text() + "\t";
    }
    ui->textEdit->appendPlainText(header);

    for(int i = 0; i < ui->tableInfo->rowCount(); ++ i)
    {
        QString content;
        for(int j = 0; j < ui->tableInfo->columnCount(); ++j)
        {
            content += ui->tableInfo->item(i, j)->text() + "\t";
        }
        ui->textEdit->appendPlainText(content);
    }
}

void MainWindow::on_tableInfo_cellClicked(int row, int column)
{
    this->labCellIndex->setText(QString("%1行 %2列").arg(row).arg(column));
    this->labStudID->setText("学生ID : " + QString::number(row + 1));
}

void MainWindow::on_tableInfo_itemClicked(QTableWidgetItem *item)
{
    int type = item->type();
    switch(type)
    {
    case MainWindow::ctName:
        this->labCellType->setText("Name");
        break;
    case MainWindow::ctSex:
        this->labCellType->setText("Sex");
        break;
    case MainWindow::ctBirth:
        this->labCellType->setText("Birth");
        break;
    case MainWindow::ctNation:
        this->labCellType->setText("Nation");
        break;
    case MainWindow::ctPartyM:
        this->labCellType->setText("PartyM");
        break;
    case MainWindow::ctScore:
        this->labCellType->setText("Score");
        break;
    }
    // this->labCellType->setText(QString::number(item->type()));
}

void MainWindow::on_chkBoxTabEditable_clicked(bool checked)
{
    if(!checked)
        ui->tableInfo->setEditTriggers(QAbstractItemView::NoEditTriggers);
    else
        ui->tableInfo->setEditTriggers(QAbstractItemView::DoubleClicked);
}

void MainWindow::on_chkBoxRowColor_clicked(bool checked)
{
    ui->tableInfo->setAlternatingRowColors(checked);
}

void MainWindow::on_chkBoxHeaderH_clicked(bool checked)
{
    ui->tableInfo->horizontalHeader()->setVisible(checked);
}

void MainWindow::on_chkBoxHeaderV_clicked(bool checked)
{
    ui->tableInfo->verticalHeader()->setVisible(checked);
}

void MainWindow::on_rBtnSelectRow_clicked()
{
    ui->tableInfo->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
}

void MainWindow::on_rBtnSelectItem_clicked()
{
    ui->tableInfo->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectItems);
}

